package com.example.springboot.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.resp.Result;
import com.example.springboot.common.utils.JwtToken;
import com.example.springboot.system.entity.HealthEntity;
import com.example.springboot.system.mapper.HealthMapper;
import com.example.springboot.system.service.HealthService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 体检表 前端控制器
 * </p>
 *
 * @author jiang
 * @since 2025-04-17
 */
@RestController
@RequestMapping("/healthEntity")
public class HealthController {

    private final HealthService service;
    private final HealthMapper mapper;

    public HealthController(HealthService service, HealthMapper mapper) {
        this.service = service;
        this.mapper = mapper;
    }

    @JwtToken
    @Operation(summary = "列表")
    @GetMapping("/healthList")
    public Result<?> healthList() {
        return Result.success(service.list());
    }

    @JwtToken
    @Operation(summary = "获取")
    @PostMapping("/healthId")
    public Result<?> healthId(@RequestParam Integer id) {
        return Result.success(mapper.selectById(id));
    }

    @JwtToken
    @Operation(summary = "列表分页")
    @PostMapping("/healthListPage")
    public Result<?> healthListPage(@RequestParam Integer page, @RequestParam Integer pageSize) {
        Page<HealthEntity> rowPage = new Page<>(page, pageSize);
        QueryWrapper<HealthEntity> wrapper = new QueryWrapper<>();
        rowPage = mapper.selectPage(rowPage, wrapper);
        return Result.success(rowPage);
    }

    @Data
    static class Health {
        private Integer userId;
        private Date checkDate;
        private Float height;
        private Float weight;
        private Float bloodPressureSystolic;
        private Float bloodPressureDiastolic;
        private Integer heartRate;
        private String bloodType;
        private Float bloodGlucose;
        private Float cholesterol;
        private Float triglycerides;
        private String liverFunction;
        private String kidneyFunction;
        private String otherFindings;
        private String doctorAdvice;
        private Date current;
    }

    @JwtToken
    @Operation(summary = "新增")
    @PostMapping("/healthAppend")
    public Result<?> healthAppend(@RequestBody Health health) {
        HealthEntity entity = new HealthEntity();
        BeanUtils.copyProperties(health, entity);
        return Result.success(service.save(entity));
    }

    @JwtToken
    @Operation(summary = "删除")
    @PostMapping("/healthDelete")
    public Result<?> healthDelete(@RequestParam Integer id) {
        return Result.success(service.removeById(id));
    }

    @JwtToken
    @Operation(summary = "编辑")
    @PostMapping("/healthEditor")
    public Result<?> healthEditor(@RequestBody HealthEntity healthEntity) {
        QueryWrapper<HealthEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("id", healthEntity.getId());
        return Result.success(service.updateById(healthEntity));
    }

    @JwtToken
    @Operation(summary = "搜索")
    @PostMapping("/healthSearch")
    public Result<?> healthSearch(@RequestParam String name) {
        QueryWrapper<HealthEntity> wrapper = new QueryWrapper<>();
        wrapper.like("name", name);
        return Result.success(mapper.selectList(wrapper));
    }


}
